package com.liangtian.mdm.material.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liangtian.mdm.activiti.api.entity.RunState;
import com.liangtian.mdm.activiti.api.entity.ShForm;
import com.liangtian.mdm.base.utils.ShiroSessionUtils;
import com.liangtian.mdm.dm.jsp.common.ExcelUtil;
import com.liangtian.mdm.material.common.constant.Constants;
import com.liangtian.mdm.material.common.util.ExcelExportUtil;
import com.liangtian.mdm.material.dao.WlLbDao;
import com.liangtian.mdm.material.model.WlLb;
import com.liangtian.mdm.material.model.WlLbUpdate;
import com.liangtian.mdm.material.service.WlLbService;
import com.liangtian.mdm.material.service.WlLbUpdateService;
import com.liangtian.mdm.material.service.WlwlService;
import com.liangtian.mdm.system.business.dao.SysBusinessDao;
import com.liangtian.mdm.system.business.model.SysBusiness;
import com.liangtian.mdm.system.mdmDis.target.model.DisService;
import com.liangtian.mdm.system.mdmDis.target.model.DisServiceCondition;
import com.liangtian.mdm.system.mdmDis.target.service.DispatchTargetManager;
import com.liangtian.mdm.system.user.model.SysUser;
import com.liangtian.mdm.util.ConstantBean;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WlLbServiceImpl implements WlLbService {

    @Autowired
    private WlLbDao wlLbDao;

    // Session中保存的当前登录用户的信息
    //@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    @Autowired
    private SysBusinessDao sysBusinessDao;

    @Autowired
    private DispatchTargetManager dispatchTargetManager;

    // @Autowired
    // private WorkFlowService workFlowService;

    @Autowired
    private WlLbUpdateService wlLbUpdateService;

    @Autowired
    private WlwlService wlwlService;

    @Override
    public List<WlLb> getWllbTreeByPermissionCond(WlLb wllb) {
        return wlLbDao.getWllbTreeByPermissionCond(wllb);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public PageInfo getWlLbListByCond(Map<String, Object> map) {
        PageInfo<List<WlLb>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            List<WlLb> wlLbList = wlLbDao.getWlLbListByCond(map);
            pageInfo = new PageInfo(wlLbList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public int insertWlLb(WlLb wllb) {
        return wlLbDao.insertWlLb(wllb);
    }

    @Override
    public int checkWllbbmExist(WlLb wllb) {
        Map<String, Object> map = new HashMap<>();
        map.put("lbbm", wllb.getLbbm());
        return wlLbDao.checkWllbbmExist(map);
    }

    @Override
    public int checkWllbmcExist(WlLb wllb) {
        Map<String, Object> map = new HashMap<>();
        map.put("lbmc", wllb.getLbmc());
        return wlLbDao.checkWllbmcExist(map);
    }

    @Override
    public int deleteWlLbByIdS(Map<String, Object> map) {
        return wlLbDao.deleteWlLbByIdS(map);
    }

    @Override
    public WlLb getWlLbById(WlLb wllb) {
        return wlLbDao.getWlLbById(wllb);
    }

    @Override
    public int checkCodeUpdate(WlLb wllb) {
        Map<String, Object> map = new HashMap<>();
        map.put("lbbm", wllb.getLbbm());
        map.put("lbbmId", wllb.getLbbmId());
        return wlLbDao.checkCodeUpdate(map);
    }

    @Override
    public int checkNameUpdate(WlLb wllb) {
        Map<String, Object> map = new HashMap<>();
        map.put("lbmc", wllb.getLbmc());
        map.put("lbbmId", wllb.getLbbmId());
        return wlLbDao.checkNameUpdate(map);
    }

    @Override
    public void updateWlLb(WlLb wllb) {
        wlLbDao.updateWlLb(wllb);

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public PageInfo getWllbQtSelectListByCond(Map<String, Object> map) {
        PageInfo<List<WlLb>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）
            List<WlLb> wlLbListByUpd = wlLbDao.getWllbQtSelectListByCond(map);
            pageInfo = new PageInfo(wlLbListByUpd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public int insertWllbUpdateBg(String lbbm_id, String operType) {
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        Map<String, Object> map = new HashMap<>();
        map.put("lbbmId", lbbm_id);
        map.put("creatorId", String.valueOf(user.getUserId()));
        map.put("creatorName", user.getUserName());
        map.put("operationtype", operType);
        map.put("state", "0");
        return wlLbDao.insertWllbUpdateBg(map);
    }

    @Override
    public int insertWllbUpdateQt(String lbbm_id, String operType) {
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);
        Map<String, Object> map = new HashMap<>();
        map.put("lbbmId", lbbm_id);
        map.put("creatorId", String.valueOf(user.getUserId()));
        map.put("creatorName", user.getUserName());
        map.put("operationtype", operType);
        map.put("state", "3");
        wlLbDao.insertWllbUpdateQt(map);
        return 1;
    }

    @Override
    public int getEditCountByLbbm(WlLb wllb) {
        Map<String, Object> map = new HashMap<>();
        map.put("lbbm", wllb.getLbbm());
        return wlLbDao.getEditCountByLbbm(map);
    }

    @Override
    public int updateWlLbByWork(Map<String, Object> map) {
        return wlLbDao.updateWlLbByWork(map);
    }

    @Override
    public void insertWllbDis(List<String> ids, String opertype) {
        Map<String, Object> dmap = new HashMap<>();
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);//操作用户
        for (int i = 0; i < ids.size(); i++) {
            String id = ids.get(i);
            WlLb wllb = new WlLb();
            wllb.setLbbmId(id);
            WlLb wlLb = this.getWlLbById(wllb);
            SysBusiness sysb = new SysBusiness();
            sysb.setBusTable(Constants.WLLB);
            List<SysBusiness> bList = sysBusinessDao.queryBusByContion(sysb);//查询业务类型
            String bus_id = bList.get(0).getBusId();//业务类型
            DisServiceCondition conditon = new DisServiceCondition();
            conditon.setSurvey_type_id(bus_id);
            if (user == null) {//万达工作流和OA集成，审核通过之后调用MDM服务端进行后续业务操作，所以获取当前用户为空
                conditon.setCreator_id("");
            } else {
                conditon.setCreator_id(user.getUserId() + "");
            }
            List<DisService> services = dispatchTargetManager.query_disService(conditon);
            if (!CollectionUtils.isEmpty(services)) {
                for (DisService vo : services) {
                    dmap.put("lbbm_id", id);
                    dmap.put("target_id", vo.getTarget_id());
                    dmap.put("target_code", vo.getTarget_code());
                    dmap.put("creator_id", wlLb.getCreatorId());
                    dmap.put("survey_type", Constants.WLLB);
                    dmap.put("oper_type", opertype);
                    wlLbDao.insertWllbDis(dmap);
                }
            }
        }

    }

    @Override
    public void insertWllbDis(String id, String opertype) {
        Map<String, Object> dmap = new HashMap<>();
        SysUser user = ShiroSessionUtils.getSessionSysUser(ConstantBean.CURRENT_SESSION_USER);//操作用户
        WlLb wllb = new WlLb();
        wllb.setLbbmId(id);
        WlLb wlLb = this.getWlLbById(wllb);
        SysBusiness sysb = new SysBusiness();
        sysb.setBusTable(Constants.WLLB);
        List<SysBusiness> bList = sysBusinessDao.queryBusByContion(sysb);//查询业务类型
        String bus_id = bList.get(0).getBusId();//业务类型
        DisServiceCondition conditon = new DisServiceCondition();
        conditon.setSurvey_type_id(bus_id);
        conditon.setCreator_id(user.getUserId() + "");
        List<DisService> services = dispatchTargetManager.query_disService(conditon);
        if (!CollectionUtils.isEmpty(services)) {
            for (DisService vo : services) {
                dmap.put("lbbm_id", id);
                dmap.put("target_id", vo.getTarget_id());
                dmap.put("target_code", vo.getTarget_code());
                dmap.put("creator_id", wlLb.getCreatorId());
                dmap.put("survey_type", Constants.WLLB);
                dmap.put("oper_type", opertype);
                wlLbDao.insertWllbDis(dmap);
            }
        }

    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public PageInfo getWllbListByCondSqSh(Map<String, Object> map) {
        PageInfo<List<WlLb>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            List<WlLb> wlLbList = wlLbDao.getWllbListByCondSqSh(map);
            pageInfo = new PageInfo(wlLbList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public int submitWlLbUpdateStateById(Map<String, Object> map) {
        return wlLbDao.submitWlLbUpdateStateById(map);
    }

    @Override
    public Map<String, Object> wlLbApprove(List<ShForm> forms) {
        Map<String, Object> result = new HashMap<>();
        boolean flag = true;//默认整个审核流程通过

        List<ShForm> sh_forms;
        try {
            // sh_forms = workFlowService.approveFlowElse(forms);
            sh_forms = new ArrayList<>();
            List<ShForm> T_data = new ArrayList<ShForm>();//审批通过且流程走完的数据
            List<ShForm> F_data = new ArrayList<ShForm>();//审批拒绝的数据
            int succCount = forms.size();//审核成功数量
            for (int i = 0; i < sh_forms.size(); i++) {
                RunState runState = sh_forms.get(i).getRunState();
                switch (runState) {
                    case PASSED:
                        T_data.add(sh_forms.get(i));
                        break;
                    case REFUSED:
                        F_data.add(sh_forms.get(i));
                        break;
                    case ERROR:
                        succCount--;//将审核流程运行错误的数据减掉
                    default:
                        break;
                }

            }
            //审核流程走完并且审核通过的数据
            if (!CollectionUtils.isEmpty(T_data)) {
                this.shenHetg(T_data);
            }
            //审核拒绝的数据，更新到物料导入表
            if (!CollectionUtils.isEmpty(F_data)) {
                this.shenHejj(F_data);
            }
            result.put("succCount", succCount);
        } catch (Exception e) {
            flag = false;
            result.put("msg", "审核出错，请联系管理员");
            e.printStackTrace();
        }
        result.put("flag", flag);
        return result;
    }

    //审核通过处理
    public void shenHetg(List<ShForm> T_data) {
        List<ShForm> a_form = new ArrayList<ShForm>();//新增物料
        List<ShForm> u_form = new ArrayList<ShForm>();//变更物料
        List<ShForm> ac_form = new ArrayList<ShForm>();//解冻数据
        List<ShForm> fr_form = new ArrayList<ShForm>();//冻结数据

        for (int k = 0; k < T_data.size(); k++) {
            ShForm shForm = T_data.get(k);
            if ("submit".equals(shForm.getOperType()))
                a_form.add(shForm);
            else if ("update".equals(shForm.getOperType()))
                u_form.add(shForm);
            else if ("active".equals(shForm.getOperType()))
                ac_form.add(shForm);
            else if ("freeze".equals(shForm.getOperType()))
                fr_form.add(shForm);
        }

        //生成物料类别
        if (a_form.size() > 0) {
            List<String> lbids = new ArrayList<String>();
            for (int a = 0; a < a_form.size(); a++) {
                ShForm shForm = T_data.get(a);
                lbids.add(shForm.getDataId());
            }
            Map<String, Object> cond = new HashMap<>();
            cond.put("state", 4);
            cond.put("lbids", lbids);
            wlLbDao.updateWlLbByWork(cond);

            //向分发表插入数据
            this.insertWllbDis(lbids, Constants.OPER_TJ);
        }

        //变更
        if (u_form.size() > 0) {
            String actiontype = "2";//操作类型  1:新增，2:修改，3:启用，4:停用
            this.update(u_form, actiontype);
        }
        //解冻
        if (ac_form.size() > 0) {
            String actiontype = "3";//操作类型  1:新增，2:修改，3:启用，4:停用
            this.update(ac_form, actiontype);
        }
        //冻结
        if (fr_form.size() > 0) {
            String actiontype = "4";//操作类型  1:新增，2:修改，3:启用，4:停用
            this.update(fr_form, actiontype);
			/*//修改变更表状态
			List<String> idList = new ArrayList<String>();
		    for (ShForm dj : fr_form) {
			    idList.add(dj.getDataId());
			}

		    Map<String, Object> cond = new HashMap<>();
			cond.put("state", 4);
			cond.put("idList", idList);
			wlLbDao.submitWlLbUpdateStateById(cond);

		    for (int i = 0; i < fr_form.size(); i++) {
				WlLbUpdate update = new WlLbUpdate();
		    	update.setUpdateId(fr_form.get(i).getDataId());
				WlLbUpdate upd = wlLbUpdateService.getWlLbUpdateById(update);
				String lbbm = upd.getLbbm();
				List<String> bmId = wlLbDao.getWlLbLbbmIdByLbbmS(lbbm);
				//批量停用
				Map<String, Object> map = new HashMap<>();
				map.put("nouse", "1");
				map.put("idList", bmId);
				wlLbDao.tyWlLbByIdS(map);

				//向分发表插入数据
				this.insertWllbDis(bmId, Constants.OPER_TY);

				//更新正式物料数据
				Map<String, Object> wlmap = new HashMap<>();
				wlmap.put("status", "0");
				wlmap.put("wllbidArray",bmId);
				wlwlService.updateWlwlStatus(wlmap);

				//向分发表插入数据
				wlwlService.insertWlwlDis(bmId, "4");
			}*/

        }
    }

    //审核拒绝处理
    public void shenHejj(List<ShForm> F_data) {
        List<ShForm> a_form = new ArrayList<ShForm>();//新增物料
		/*List<ShForm> u_form = new ArrayList<ShForm>();//变更物料
		List<ShForm> ac_form = new ArrayList<ShForm>();//解冻数据
		List<ShForm> fr_form = new ArrayList<ShForm>();//冻结数据
		 */
        List<ShForm> shLst = new ArrayList<ShForm>();//待更新物料变更表数据
        for (int k = 0; k < F_data.size(); k++) {
            ShForm shForm = F_data.get(k);
            if ("submit".equals(shForm.getOperType()))
                a_form.add(shForm);
            else
                shLst.add(shForm);
			/*else if("update".equals(shForm.getOperType()))
				u_form.add(shForm);
			else if("active".equals(shForm.getOperType()))
				ac_form.add(shForm);
			else if("freeze".equals(shForm.getOperType()))
				fr_form.add(shForm);*/
        }

        //更新物料类别表
        if (a_form.size() > 0) {
            List<String> idList = new ArrayList<String>();
            for (int i = 0; i < a_form.size(); i++) {
                ShForm shForm = a_form.get(i);
                idList.add(shForm.getDataId());
            }
            Map<String, Object> cond = new HashMap<>();
            cond.put("state", 6);
            cond.put("lbids", idList);
            wlLbDao.updateWlLbByWork(cond);
        }
        if (shLst.size() > 0) {
            List<String> idList = new ArrayList<String>();
            for (int i = 0; i < shLst.size(); i++) {
                ShForm shForm = shLst.get(i);
                idList.add(shForm.getDataId());
            }
            Map<String, Object> cond = new HashMap<>();
            cond.put("state", 6);
            cond.put("idList", idList);
            wlLbUpdateService.updateWllbStateByCond(cond);
			/*//变更
			if(u_form.size()>0){

			}
			//解冻
			if(ac_form.size()>0){

			}
			//冻结
			if(fr_form.size()>0){

			}*/
        }

    }

    /**
     * @param sh_list          审核数据
     * @param actiontype//操作类型 1:新增，2:修改，3:启用，4:停用
     */
    private void update(List<ShForm> sh_list, String actiontype) {
        //修改物料类别变更表状态
        List<String> idList = new ArrayList<String>();
        for (ShForm dj : sh_list) {
            idList.add(dj.getDataId());
        }
        Map<String, Object> cond = new HashMap<>();
        cond.put("state", 4);
        cond.put("updIdList", idList);
        wlLbDao.submitWlLbUpdateStateById(cond);//修改物料类别变更表状态

        Map<String, Object> upd_cond = new HashMap<>();
        upd_cond.put("idList", idList);
        List<WlLbUpdate> list = wlLbUpdateService.getWllbUpdateListByCond(upd_cond);

        if (!CollectionUtils.isEmpty(list)) {
            String oper = Constants.OPER_BG;//操作类型

            //变更
            if ("2".equals(actiontype)) {

                List<String> newupdateIds = shenHePass(idList);
                List<String> lbbmIdLst = new ArrayList<String>();
                for (String updateId : newupdateIds) {
                    WlLbUpdate update = new WlLbUpdate();
                    update.setUpdateId(updateId);
                    WlLbUpdate upd = wlLbUpdateService.getWlLbUpdateById(update);
                    lbbmIdLst.add(upd.getLbbmId());
                }

                //向分发表插入数据
                this.insertWllbDis(lbbmIdLst, Constants.OPER_BG);
                //向分发表插入数据
                //wlwlService.insertWlwlDis(lbbmIdLst, "2");

	    		/*wlLbUpdateService.updateWllbByUpdate(upd_cond);
	    		//向分发表插入数据
				this.insertWllbDis(idList, oper);*/
            } else {
                //冻结解冻
                String nouse = "0";//是否停用标识  0：启用，1：停用
                String status = "1";//物料表是否停用标识  1：启用，0：停用
                //解冻
                if ("3".equals(actiontype)) {
                    oper = Constants.OPER_QY;
                }
                //冻结
                if ("4".equals(actiontype)) {
                    oper = Constants.OPER_TY;
                    nouse = "1";
                    status = "0";
                }

                //批量冻结解冻 （冻结解冻大类或中类时将所属中类、小类同时冻结解冻）
                for (WlLbUpdate wllbUpdate : list) {
                    String lbbm = wllbUpdate.getLbbm();
                    List<String> ids = wlLbDao.getWlLbLbbmIdByLbbmS(lbbm);
                    Map<String, Object> map = new HashMap<>();
                    map.put("nouse", nouse);
                    map.put("idList", ids);
                    wlLbDao.tyWlLbByIdS(map);

                    //向分发表插入数据
                    this.insertWllbDis(ids, oper);

                    //更新正式物料数据
                    Map<String, Object> wlmap = new HashMap<>();
                    wlmap.put("status", status);
                    wlmap.put("wllbidArray", ids);
                    wlwlService.updateWlwlStatus(wlmap);

                    //向分发表插入数据
                    //wlwlService.insertWlwlDis( , actiontype);
                }
            }

        }
    }

    @Override
    public List<String> getWlLbLbbmIdByLbbmS(String lbbm) {
        return wlLbDao.getWlLbLbbmIdByLbbmS(lbbm);
    }

    @Override
    public void tyWlLbByIdS(Map<String, Object> map) {
        wlLbDao.tyWlLbByIdS(map);

    }

    @Override
    public void updateWlLbBg(WlLb wllb) {
        wlLbDao.updateWlLbBg(wllb);
    }

    @Override
    public void updateWlLbBgLong(WlLb wllb) {
        wlLbDao.updateWlLbBgLong(wllb);
    }

    /**
     * 将变更审核通过的数据更新到正式表,并且获得除修改产品组以外字段的ID集合
     *
     * @param updateIds
     */
    private List<String> shenHePass(List<String> updateIds) {
        List<String> updateNewIds = new ArrayList<String>();
        for (String updateId : updateIds) {
            WlLbUpdate upd = new WlLbUpdate();
            upd.setUpdateId(updateId);
            WlLbUpdate update = wlLbUpdateService.getWlLbUpdateById(upd);//审核通过后待更新的数据
            WlLb querywllb = new WlLb();
            querywllb.setLbbmId(update.getLbbmId());
            WlLb wlLb2 = this.getWlLbById(querywllb);//没更新之前的数据
            updateNewIds.add(updateId);
            WlLb wllb = new WlLb();
            if (String.valueOf(update.getVersion()).equals("1")) {
                WlLbUpdate update2 = new WlLbUpdate();
                update2.setLbbm(wlLb2.getLbbm());
                update2.setPid(wlLb2.getPid());
                update2.setLbmc(wlLb2.getLbmc());
                update2.setPid(wlLb2.getPid());
                update2.setLbbmId(wlLb2.getLbbmId());
                update2.setBzsm(wlLb2.getBzsm());
                update2.setJldw(wlLb2.getJldw());
                update2.setLmzwm(wlLb2.getLmzwm());
                update2.setWzcpbz(wlLb2.getWzcpbz());
                update2.setBmgz(wlLb2.getBmgz());
                update2.setCpgroup(wlLb2.getCpgroup());
                update2.setCglx(wlLb2.getCglx());
                update2.setState("4");
                update2.setVersion(wlLb2.getVersion());
                update2.setCreateTime(wlLb2.getCreateTime());
                update2.setCreatorId(wlLb2.getCreatorId());
                update2.setCreatorName(wlLb2.getCreatorName());
                update2.setAllownextlevel(wlLb2.getAllowNextLevel());
                update2.setOperationtype("0");
                update2.setLbmcjc(wlLb2.getLbmcjc());
                update2.setNormt1(wlLb2.getNormt1());
                update2.setNormt2(wlLb2.getNormt2());
                update2.setNormt3(wlLb2.getNormt3());
                update2.setNormt4(wlLb2.getNormt4());
                update2.setSubmitTime(wlLb2.getSubmitTime());
                wlLbUpdateService.insertWlLbUpdate(update2);  //将正式表数据插入变更表
            }
            if (update.getLbbm().length() == 2) {
                wllb.setLbmc(update.getLbmc());
                wllb.setBzsm(update.getBzsm());
                wllb.setLbbmId(update.getLbbmId());
                wllb.setUpdateTime(update.getCreateTime());
                wllb.setUpdateUid(update.getCreatorId());
                wllb.setUpdateUname(update.getCreatorName());
                wllb.setVersion(wlLb2.getVersion() + 1);
                wllb.setLbmcjc(update.getLbmcjc());
                wllb.setNormt1(update.getNormt1());
                wllb.setNormt2(update.getNormt2());
                wllb.setNormt3(update.getNormt3());
                wllb.setNormt4(update.getNormt4());
                this.updateWlLbBg(wllb);
            } else {
                wllb.setLbmc(update.getLbmc());
                wllb.setBzsm(update.getBzsm());
                wllb.setLbbmId(update.getLbbmId());
                wllb.setJldw(update.getJldw());
                wllb.setWzcpbz(update.getWzcpbz());
                wllb.setCglx(update.getCglx());
                wllb.setUpdateTime(update.getCreateTime());
                wllb.setUpdateUid(update.getCreatorId());
                wllb.setUpdateUname(update.getCreatorName());
                wllb.setVersion(wlLb2.getVersion() + 1);
                wllb.setLbmcjc(update.getLbmcjc());
                wllb.setNormt1(update.getNormt1());
                wllb.setNormt2(update.getNormt2());
                wllb.setNormt3(update.getNormt3());
                wllb.setNormt4(update.getNormt4());
                wllb.setCpgroup(update.getCpgroup());//添加修改产品组方法
                this.updateWlLbBgLong(wllb);
            }
        }
        return updateNewIds;
    }

    @Override
    public List<WlLb> getWllbTreeByTzlQzKey(Map<String, Object> map) {
        return wlLbDao.getWllbTreeByTzlQzKey(map);
    }

    @Override
    public List<WlLb> getWlLbExportListByCond(Map<String, Object> map) {
        return wlLbDao.getWlLbListByCond(map);
    }

    @Override
    public void exportWlLb(List<WlLb> list, HttpServletResponse response) {
        try {
            String tablename = "物料类别";
            // 声明一个工作薄
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("物料类别");
            XSSFRow row = sheet.createRow(0);
            XSSFCell cell = null;
            CellStyle style = ExcelUtil.createCellTextStyle(workbook);
            cell = row.createCell(0);
            sheet.setDefaultColumnStyle(0, style);
            cell.setCellValue("类别代码");
            cell = row.createCell(1);
            sheet.setDefaultColumnStyle(1, style);
            cell.setCellValue("类别名称");
            cell = row.createCell(2);
            sheet.setDefaultColumnStyle(2, style);
            cell.setCellValue("类型");
            cell = row.createCell(3);
            sheet.setDefaultColumnStyle(3, style);
            cell.setCellValue("计量单位");
            cell = row.createCell(4);
            sheet.setDefaultColumnStyle(4, style);
            cell.setCellValue("编制说明");
            cell = row.createCell(5);
            sheet.setDefaultColumnStyle(5, style);
            cell.setCellValue("状态");
            cell = row.createCell(6);
            sheet.setDefaultColumnStyle(6, style);
            cell.setCellValue("创建人");
            cell = row.createCell(7);
            sheet.setDefaultColumnStyle(7, style);
            cell.setCellValue("创建时间");
            cell = row.createCell(8);
            sheet.setDefaultColumnStyle(8, style);
            cell.setCellValue("类别");
            for (int i = 0; i < list.size(); i++) {
                WlLb getWlLb = list.get(i);
                row = sheet.createRow(i + 1);
                cell = row.createCell(0);
                cell.setCellValue(getWlLb.getLbbm());
                cell = row.createCell(1);
                cell.setCellValue(getWlLb.getLbmc());
                cell = row.createCell(2);
                cell.setCellValue(getWlLb.getWzcpbz().equals("1") ? "物资" : "产品");
                cell = row.createCell(3);
                cell.setCellValue(getWlLb.getJldw());
                cell = row.createCell(4);
                cell.setCellValue(getWlLb.getBzsm());
                cell = row.createCell(5);
                cell.setCellValue(getWlLb.getNouse().equals("1") ? "冻结" : "活动");
                cell = row.createCell(6);
                cell.setCellValue(getWlLb.getCreatorName());
                cell = row.createCell(7);
                cell.setCellValue(getWlLb.getCreateTime());
                cell = row.createCell(8);
                String leiBie = "";
                if ("2".equals(getWlLb.getNormt2())) {
                    leiBie = "大类";
                } else if ("4".equals(getWlLb.getNormt2())) {
                    leiBie = "中类";
                } else {
                    leiBie = "小类";
                }
                cell.setCellValue(leiBie);
            }
            ExcelExportUtil.outExportExcel(tablename, workbook, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public PageInfo getWlLbListByCond2(Map<String, Object> map) {
        PageInfo<List<WlLb>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            List<WlLb> wlLbList = wlLbDao.getWlLbListByCond(map);
            pageInfo = new PageInfo(wlLbList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public List<String> getTzlQzKeyLbbms(String qzKey) {
        return this.wlLbDao.getTzlQzKeyLbbms(qzKey);
    }

}
